/**
 *
 */
package org.insia.smartposse.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.form.ProfileForm;
import org.insia.smartposse.manager.UserManager;

/**
 * @author cchalloy
 *
 */
public class RegisterAction extends DispatchAction {

	public ActionForward form(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {

		return mapping.findForward("form");
	}

	public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		ProfileForm profileForm = (ProfileForm) form;
		UserManager user = new UserManager();
		user.populateLogin(profileForm);
		user.populateProfile(profileForm);
		user.save();

		// Add message for say the User is Register Good
		req.getSession().setAttribute("registerok", true);

		// Save in session user login
		req.getSession().setAttribute("user", user.getLogin());

		// Redirect to view Profile because the User is register
		resp.sendRedirect(req.getContextPath() + "/profile.do");
		return null;
	}

}
